<template>
  <div class="bg-[#232324] h-[100%]">
    <el-collapse v-loading="loading" v-model="activeNames">
      <el-form label-width="100" :model="states" size="default">
        <el-collapse-item title="组件" name="1">
          <el-form-item label="x坐标">
            <el-input-number v-model="states.x" />
          </el-form-item>
          <el-form-item label="y坐标">
            <el-input-number v-model="states.y" />
          </el-form-item>
          <el-form-item label="宽度">
            <el-input-number v-model="states.width" />
          </el-form-item>
          <el-form-item label="高度">
            <el-input-number v-model="states.height" />
          </el-form-item>

          <el-form-item label="组件名称">
            <el-input
              type="text"
              v-model="states.componentLabelName"
              @input="changeLabel"
            />
          </el-form-item>
        </el-collapse-item>
      </el-form>
    </el-collapse>
  </div>
</template>

<script setup>
import { computed, onMounted, ref } from "vue";
import { debounce } from "@renderer/utils/other";
const loading = ref(true);
const activeNames = ref("1");
const props = defineProps({
  models: {
    type: Object,
    default: () => ({
      x: 0,
      y: 0,
      width: 0,
      height: 0,
    }),
  },
  layouts: {
    type: Object,
    default: () => [],
  },
});
const emit = defineEmits(["update:models", "edit"]);
let originLabelName = "";
onMounted(() => {
  setTimeout(() => {
    loading.value = false;
  }, 200);
  originLabelName = props.models.componentLabelName;
});

const states = computed({
  get: () => props.models,
  set: () => emit("update:models"),
});

const changeLabel = debounce((val) => {
  let labelNameArr = props.layouts
    .filter((it) => it.id != states.value.id)
    .map((de) => de.componentLabelName);

  if (labelNameArr.findIndex((it) => it == val) >= 0) {
    ElMessageBox.alert("检测到组件名称有重复，请重新输入", "提示", {
      confirmButtonText: "确定",
      callback: (action) => {
        states.value.componentLabelName = originLabelName;
      },
    });
    return;
  }
}, 500);
</script>

<style scoped lang="scss">
:deep(.el-collapse-item__header) {
  padding-left: 10px !important;
}
:deep(.el-input) {
  width: 180px !important;
}
:deep(.el-select) {
  width: 180px !important;
}
</style>
